package com.haorui.lygyd.wxapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.haorui.base.system.entity.JiekouEntity;
import com.haorui.base.system.service.JiekouService;
import com.haorui.lygyd.wxapi.common.CommonUtil;
import com.haorui.lygyd.wxapi.common.WeixinUserInfo;
import com.haorui.lygyd.wxapi.entity.WXUserInfoEntity;
import com.haorui.lygyd.wxapi.service.WXUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 陈刚
 * @version 1.0
 * @project znsm
 * @package com.haorui.wxapi.controller
 * @createDate 2017-09-30 19:44
 * @modfiyDate
 * @function
 */
@RestController
@RequestMapping(value = "/auth")
@Slf4j
public class UserLoginCtrl {
    @Autowired
    private JiekouService jiekouService;
    @Autowired
    private WXUserInfoService weixinUserInfoService;
    @Value("${wx.wxLoginUrl}")
    public String wxLoginUrl;
    @Value("${wx.wxCenterUrl}")
    public String wxCenterUrl;

    @RequestMapping(value = "/userLogin")
    public void userLogin(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String code= (String) request.getParameter("code");
        if(code==null){
            return;
        }
        JiekouEntity jiekouEntity=jiekouService.findOne("1");
        String appid=jiekouEntity.getGongzhonghaoAppid();
        String appsecret=jiekouEntity.getGongzhonghaoAppSecret();
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=AppId&secret=AppSecret&code=CODE&grant_type=authorization_code";
        url = url.replace("AppId", appid)
                .replace("AppSecret", appsecret)
                .replace("CODE", code);
        JSONObject jsonTexts= CommonUtil.httpsRequest(url,"GET",null);
        String openid = null;
        if (jsonTexts.get("openid")!=null) {
            openid = jsonTexts.get("openid").toString();
            WXUserInfoEntity weixinUserInfoEntity=weixinUserInfoService.wxgetweixinUserByOpenId(openid);
            if (weixinUserInfoEntity==null)
            {
                String accessToken = CommonUtil.getToken(appid, appsecret).getAccessToken();
                WXUserInfoEntity wxGetUser = WeixinUserInfo.getUserInfo(accessToken, openid);
                if(wxGetUser!=null && wxGetUser.getOpenId()!=null){
                    weixinUserInfoEntity = weixinUserInfoService.save(wxGetUser);
                }
            }
        }

        //todo 跳转登录页面
        log.info(wxLoginUrl+openid);
        response.sendRedirect(wxLoginUrl+openid);
//        if (weixinUserInfoEntity.getMemberId()==null)
//        {
//
//        }else {
//            //todo 跳转个人主页
//            log.info(wxCenterUrl+weixinUserInfoEntity.getMemberId());
//            response.sendRedirect(wxCenterUrl+weixinUserInfoEntity.getMemberId());
//        }
        //response.sendRedirect("http://znsm.91eqy.com:3000/auth.html");

    }

}
